Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@fastify/websocket

Package Overview
Dependencies
Maintainers
20
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/websocket

basic websocket support for fastify

  • 8.3.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
264K
decreased by-19.84%
Maintainers
20
Weekly downloads
 
Created

What is @fastify/websocket?

@fastify/websocket is a Fastify plugin that provides WebSocket support. It allows you to easily integrate WebSocket functionality into your Fastify applications, enabling real-time communication between the server and clients.

What are @fastify/websocket's main functionalities?

Basic WebSocket Server

This code sets up a basic WebSocket server using Fastify and the @fastify/websocket plugin. When a client connects to the /ws endpoint, the server listens for messages and responds with a confirmation message.

const fastify = require('fastify')();
const websocket = require('@fastify/websocket');

fastify.register(websocket);

fastify.get('/ws', { websocket: true }, (connection, req) => {
  connection.socket.on('message', message => {
    connection.socket.send(`Received: ${message}`);
  });
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Handling WebSocket Errors

This code demonstrates how to handle WebSocket errors. The server listens for error events on the WebSocket connection and logs the error to the console.

const fastify = require('fastify')();
const websocket = require('@fastify/websocket');

fastify.register(websocket);

fastify.get('/ws', { websocket: true }, (connection, req) => {
  connection.socket.on('message', message => {
    connection.socket.send(`Received: ${message}`);
  });

  connection.socket.on('error', error => {
    console.error('WebSocket error:', error);
  });
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Broadcasting Messages to All Clients

This code shows how to broadcast messages to all connected WebSocket clients. When a client sends a message, the server broadcasts it to all other connected clients.

const fastify = require('fastify')();
const websocket = require('@fastify/websocket');

fastify.register(websocket);

let clients = [];

fastify.get('/ws', { websocket: true }, (connection, req) => {
  clients.push(connection.socket);

  connection.socket.on('message', message => {
    clients.forEach(client => {
      if (client !== connection.socket) {
        client.send(`Broadcast: ${message}`);
      }
    });
  });

  connection.socket.on('close', () => {
    clients = clients.filter(client => client !== connection.socket);
  });
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Other packages similar to @fastify/websocket

Keywords

FAQs

Package last updated on 26 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc